package com.tlvip.nk;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

/**
 * 来源牛客网 HJ107 求解立方根
 * <p>description：给定一个数字，求其立方根，精确到小数点后一位，double类型即可</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p></p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 2022/11/16</p>
 * Author mac
 *
 * @version 1.0
 */
public class CalSqu3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double num = in.nextDouble();
        System.out.println(getCalSqu3(num));
    }

    private static double getCalSqu3(double num) {
        if (num == 0 || num == 1 || num == -1) {
            return num;
        }
        // 如果是负数，转成正数，简化处理方式，末了将立方根转成负数
        boolean symbol = false;
        if (num < 0) {
            symbol = true;
            num = 0 - num;
        }
        // 采用二分查找法寻求最优解
        double squ3 = num / 2; // 立方根
        double half = squ3; // 半数
        while (true) {
            double temp = squ3 * squ3 * squ3;
            // 因为保留一位小数，计算时，将精度提高一位，提高计算精度
            if (temp == num || ((num - temp) <= 0.01 && (temp - num) <= 0.01)) {
                break;
            } else if (temp > num) {
                squ3 = squ3 / 2;
                half = half / 2;
            } else {
                squ3 = (squ3 + half / 2);
            }
        }
        if (symbol) {
            squ3 = 0 - squ3;
        }
        BigDecimal bb = new BigDecimal(squ3).setScale(1, RoundingMode.HALF_DOWN);
        return bb.doubleValue();
    }
}
